/**
 * @author Ahammer
 * @date   2009 06
 * @update 2010 09 eliminated normalize values for Sobel, Prewitt, FreiChen
 * @update 2010 09 added option: direction
 * @update 2011 01 added 16 bit support
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;


import java.awt.RenderingHints;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.BorderExtender;
import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

import main.IqmTools;

/**
 * This is the main image processing class
 * There exist two approaches:
 * A user defined JAI operator is just called 
 * or:
 * The actual processing is implemented in this class 
 */
public class IqmEdgeOperator{ 


  public IqmEdgeOperator() {
	  //WARNING: Don't declare fields here
	  //Fields declared here aren't thread safe!   
  }
  /**
   * 
   * @param ParametrBlockJAI pb
   */
  public Object run(ParameterBlockJAI pbJAI){
	  Object ob = null;
      //ob = JAI.create("IqmEdge".toLowerCase(), pbJAI, null);     
      
	  PlanarImage pi = (PlanarImage) pbJAI.getSource(0);
	  int method     = pbJAI.getIntParameter("Method");
	  int direction  = pbJAI.getIntParameter("Direction");
	  
	  String type = IqmTools.getImgTyp(pi);
	  double typeGreyMax = IqmTools.getImgTypeGreyMax(pi);
	  
	  KernelJAI kernel_h = null;
	  KernelJAI kernel_v = null;
	  
	  RenderingHints rh = null;	
	  ParameterBlock pb = new ParameterBlock();

//	  pb.addSource(pi);
//	  pb.add(DataBuffer.TYPE_DOUBLE);
//    pi = JAI.create("format", pb, rh);  
      
	  //-----------------------------------------------------
	  if (method == 0){ //Roberts
		  float[] h_data = { 0.0f,  0.0f, -1.0f,
		  					 0.0f,  1.0f,  0.0f,
		  					 0.0f,  0.0f,  0.0f  };
		  float[] v_data = {-1.0f,  0.0f,  0.0f,
				        	 0.0f,  1.0f,  0.0f,
				        	 0.0f,  0.0f,  0.0f  };
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);	  
	  }
	  //-----------------------------------------------------
	  if (method == 1){ //Pixel difference  see WK Pratt Digital Image Processing 2nd ed. p503
		  float[] h_data = { 0.0f,  0.0f,  0.0f,
		  					 0.0f,  1.0f, -1.0f,
		  					 0.0f,  0.0f,  0.0f  };
		  float[] v_data = { 0.0f, -1.0f,  0.0f,
				        	 0.0f,  1.0f,  0.0f,
				        	 0.0f,  0.0f,  0.0f  };
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);	  
	  }
	  //-----------------------------------------------------
	  if (method == 2){ //Separated Pixel Difference  see WK Pratt Digital Image Processing 2nd ed. p503
		  float[] h_data = { 0.0f,  0.0f,  0.0f,
		  					 1.0f,  0.0f, -1.0f,
		  					 0.0f,  0.0f,  0.0f  };
		  float[] v_data = { 0.0f, -1.0f,  0.0f,
				        	 0.0f,  0.0f,  0.0f,
				        	 0.0f,  1.0f,  0.0f  };
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);	  
	  }
	  //-----------------------------------------------------
	  if (method == 3){ //Sobel
		  float[] h_data = { 1.0f,  0.0f, -1.0f,
		  					 2.0f,  0.0f, -2.0f,
		  					 1.0f,  0.0f, -1.0f  };
		  float[] v_data = {-1.0f, -2.0f, -1.0f,
				        	 0.0f,  0.0f,  0.0f,
				        	 1.0f,  2.0f,  1.0f  };
//		  for (int i = 0; i < h_data.length; i++){
//			  h_data[i] = h_data[i]*(1.0f/4.0f);
//		  }
//		  for (int i = 0; i < v_data.length; i++){
//			  v_data[i] = v_data[i]*(1.0f/4.0f);
//		  }
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);
		  //kernel_h = KernelJAI.GRADIENT_MASK_SOBEL_HORIZONTAL;
		  //kernel_v = KernelJAI.GRADIENT_MASK_SOBEL_VERTICAL;
	  }
	  //-----------------------------------------------------
	  if (method == 4){ //Prewitt
		  float[] h_data = { 1.0f,  0.0f, -1.0f,
		  					 1.0f,  0.0f, -1.0f,
		  					 1.0f,  0.0f, -1.0f  };
		  float[] v_data = {-1.0f, -1.0f, -1.0f,
				        	 0.0f,  0.0f,  0.0f,
				        	 1.0f,  1.0f,  1.0f  };
//		  for (int i = 0; i < h_data.length; i++){
//			  h_data[i] = h_data[i]*(1.0f/3.0f);
//		  }
//		  for (int i = 0; i < v_data.length; i++){
//			  v_data[i] = v_data[i]*(1.0f/3.0f);
//		  }
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);	
	  }
	  //-----------------------------------------------------
	  if (method == 5){ //FreiChen
		  float[] h_data = { 1.0f,                 0.0f,                -1.0f,
		  					 (float)Math.sqrt(2),  0.0f,  -(float)Math.sqrt(2),
		  					 1.0f,                 0.0f,                -1.0f  };
		  float[] v_data = {-1.0f,  -(float)Math.sqrt(2),  -1.0f,
				        	 0.0f,        0.0f,             0.0f,
				        	 1.0f,   (float)Math.sqrt(2),   1.0f  };
//		  for (int i = 0; i < h_data.length; i++){
//			  h_data[i] = h_data[i]*(1.0f/(2.0f + (float)Math.sqrt(2)));
//		  }
//		  for (int i = 0; i < v_data.length; i++){
//			  v_data[i] = v_data[i]*(1.0f/(2.0f + (float)Math.sqrt(2)));
//		  }
		  kernel_h = new KernelJAI(3,3, h_data);
		  kernel_v = new KernelJAI(3,3, v_data);	
	  }
	  //-----------------------------------------------------
//	  if (method == ){ //
//		  float[] h_data = { 0.0f,  0.0f,  0.0f,
//		  					 0.0f,  0.0f,  0.0f,
//		  					 0.0f,  0.0f,  0.0f  };
//		  float[] v_data = { 0.0f,  0.0f,  0.0f,
//				        	 0.0f,  0.0f,  0.0f,
//				        	 0.0f,  0.0f,  0.0f  };
//		  kernel_h = new KernelJAI(3,3, h_data);
//		  kernel_v = new KernelJAI(3,3, v_data);	  
//	  }
	
	  //-----------------------------------------------------

	  
	  //int kernelSize  = pbJAI.getIntParameter("KernelSize");
 
	  rh = new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY));
	  pb = new ParameterBlock();
      pb.addSource(pi);
      if (direction == 0){ //standard
    	  pb.add(kernel_h);
    	  pb.add(kernel_v);
    	  ob = JAI.create("GradientMagnitude", pb, rh);
      }
      if (direction == 1){ //horizontal
    	  pb.add(kernel_h);	 
    	  ob = JAI.create("Convolve", pb, rh);
      }
      if (direction == 2){ //vertical
    	  pb.add(kernel_v);	 
    	  ob = JAI.create("Convolve", pb, rh);
      }

	   
	  //Handling of result
	  //--------------------------------------------------------------------
	  int resultOptions = 2;
	  
	  if (resultOptions == 0){ //clamp to byte
		  pb = new ParameterBlock();
		  pb.addSource(ob);
		  if (type == "RGB")    pb.add(DataBuffer.TYPE_BYTE);
		  if (type == "8 bit")  pb.add(DataBuffer.TYPE_BYTE);
		  if (type == "16 bit") pb.add(DataBuffer.TYPE_USHORT);
		  ob = JAI.create("format", pb);  	  
	  }
	  if (resultOptions == 1){ //normalize to byte
	        pb = new ParameterBlock();
			pb.addSource(ob);
			RenderedOp extrema = JAI.create("extrema", pb);
		    double[] minVec = (double[])extrema.getProperty("minimum");
		    double[] maxVec = (double[])extrema.getProperty("maximum");
		    double min = minVec[0];
		    double max = maxVec[0];
		
		    pb = new ParameterBlock();
			pb.addSource(ob);
		    pb.add(new double[] {(typeGreyMax/(max-min))}); //Rescale 
		    pb.add(new double[] {((typeGreyMax)/(min-max))}); //offset
			ob = JAI.create("rescale", pb); 
		 
			pb = new ParameterBlock();
			pb.addSource(ob);
			if (type == "RGB")    pb.add(DataBuffer.TYPE_BYTE);
			if (type == "8 bit")  pb.add(DataBuffer.TYPE_BYTE);
			if (type == "16 bit") pb.add(DataBuffer.TYPE_USHORT);
			ob = JAI.create("format", pb);  	 
	  }

	  if (resultOptions == 2){ //Actual
		  //does nothing
	  }
			
	  return ob;
  }
}
